package com.cloudkinto.cron;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.cloudkinto.common.RedisKeyConstants;
import com.cloudkinto.entity.ClaimDo;
import com.cloudkinto.entity.ProductStockRackDo;
import com.cloudkinto.entity.StorageLocationDo;
import com.cloudkinto.enumType.claim.ClaimStatusEnum;
import com.cloudkinto.service.job.AsyncJobService;
import com.cloudkinto.service.mail.MailService;
import com.cloudkinto.service.order.claim.ClaimService;
import com.cloudkinto.service.order.outbound.OutboundService;
import com.cloudkinto.service.platform.*;
import com.cloudkinto.service.platform.qoo10.PlatformOrderQoo10Service;
import com.cloudkinto.service.stock.ProductStockRackService;
import com.cloudkinto.service.stock.ProductStockService;
import com.cloudkinto.service.storage.StorageLocationService;
import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.stream.Collectors;


/**
 * @author zhangyy
 * @date 2020/6/18 16:03
 * @description: 生产环境下的定时任务
 */
@ConditionalOnProperty(prefix = "gbc", name = "scheduling", havingValue = "prod")
@Component
@RequiredArgsConstructor
@Logger
public class CronProdService {
    private final ProductStockService productStockService;
    private final RedisTemplate redisTemplate;
    private final ClaimService claimService;
    private final PlatformOrderAmazonSyncService platformOrderAmazonSyncService;
    private final PlatformOrderRakutenSyncService platformOrderRakutenSyncService;
    private final PlatformOrderShopifySyncService platformOrderShopifySyncOrderService;
    private final PlatformOrderMakeshopSyncService platformOrderMakeshopSyncService;
    private final PlatformOrderQoo10Service platformOrderQoo10Service;
    private final PlatformOrderYahooSyncService platformOrderYahooSyncService;
    private final MailService mailService;
    private final PlatformOrderWowmaSyncService platformOrderWowmaSyncService;
    private final ProductStockRackService stockRackService;
    private final StorageLocationService storageLocationService;
    private final OutboundService outboundService;
    private final PlatformOrderService platformOrderService;
    private final AsyncJobService asyncJobService;

//    @Scheduled(fixedRate = 1000 * 60 * 60 * 80)
//    public void wowmaSyncItems() {
//        platformOrderWowmaSyncService.syncItems();
//    }

    /**
     * 每天凌晨重置上一次库存回传记录(缓存)
     */
//    @Scheduled(cron = "58 30 0 * * ?")
//    public void resetStockToPlatformRecord() {
//        platformOrderService.resetStockToPlatformRecord();
//    }


    /**
     * 每天定时删除 库存回传 10天之前的数据
     */

    @Scheduled(cron = "10 22 0 * * ?")
    public void deleteStockToPlatformRecord() {
        asyncJobService.deleteStockToPlatformRecord();

    }

    /**
     * 每天凌晨 重置同步时间
     */
    @Scheduled(cron = "10 0 0/4 * * ?")
    public void resetRedisSyncTime() {
        platformOrderService.resetRedisSyncTime(true);
    }



    /**
     * 自动审核订单
     */
    @Scheduled(cron = "00 */5 * * * ?")
//    @Scheduled(cron = "50 35 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void platformOrderAutoCheck() {
        //只审核 未审核过的订单
        platformOrderService.autoCheck("1");
    }

    /**
     * 自动审核订单
     */
    @Scheduled(cron = "30 37 */3 * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void platformOrderAutoCheck1() {
        //只审核 审核过的订单
        platformOrderService.autoCheck("0");
    }



    /**
     * 自动标发
     */
    @Scheduled(cron = "50 18,38,58 * * * ?")
    public void platformOrderAutoMarkShip() {
        platformOrderService.autoMarkShip();
    }


    /**
     * 获取jdy单号
     */
    @Scheduled(cron = "13 07 8-22 * * ?")
//    @Scheduled(cron = "13 05,25,45 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void getJdyTrackNumber() {
        outboundService.getJdyTrackNumber();
    }


    /**
     * 一件代发推动订单到jdy
     */
    @Scheduled(cron = "50 18,48 * * * ?")
    public void pushToJdCloud() {
        outboundService.pushToJdCloud();
    }


    @Scheduled(cron = "40 04 2,14 * * ?")
    public void wowmaSearchItemInfos() {
        platformOrderWowmaSyncService.syncItems();
    }


    @Scheduled(cron = "40 04 1,13 * * ?")
    public void qoo10GoodsInfo() {
        platformOrderQoo10Service.getAllGoodsInfo();
    }


    @Scheduled(cron = "40 5 0 */3 * ?")
    public void yahooGetItemList() {
        platformOrderYahooSyncService.getItemList();
    }

    @Scheduled(cron = "40 10 0 */3 * ?")
    public void rakutenGetItemList() {
        platformOrderRakutenSyncService.itemSearch();
    }

    /**
     * 获取乐天平台库存。暂时不要了
     */
    @Scheduled(cron = "40 20 0 * * ?")
    public void rakutenGetInventoryList() {
//        platformOrderRakutenSyncService.inventoryGet();
    }

    @Scheduled(cron = "40 15 0 */3 * ?")
    public void AmazonGetItemList() {
        platformOrderAmazonSyncService.amazonGetItemList();
    }


    /**
     * 库存回传至平台
     */
//    @Scheduled(cron = "08 08,28,48 * * * ?")
//    public void stockToPlatform() {
//        productStockService.stockToPlatform();
//    }


    /**
     * Wowma 标发完成 标记完成
     */
    @Scheduled(cron = "23 07 * * * ?")
    public void makeStatusFinish() {
        platformOrderWowmaSyncService.makeFinish();
    }


    /**
     * 自动同步wowma订单
     */
    @Scheduled(cron = "40 04,24,44 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void wowmaOrderSync() {
        platformOrderWowmaSyncService.platOrderWowmaSync();
    }


    /**
     * Yahoo 标发完成 标记完成
     */
    @Scheduled(cron = "23 05 * * * ?")
    public void makeFinish() {
        platformOrderYahooSyncService.makeFinish();
    }


    /**
     * 自动同步yahoo订单
     */
    @Scheduled(cron = "05 11,31,51 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 20)
    public void yahooOrderSync() {
        platformOrderYahooSyncService.platOrderYahooSync();
    }



    /**
     * 定时同步乐天标发状态
     */
    @Scheduled(cron = "33 0/15 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 10)
    public void syncMarkRecordRakuten() {
        platformOrderRakutenSyncService.syncMarkRecord();
    }

    /**
     * 定时同步亚马逊标发状态
     */
    @Scheduled(cron = "13 0/15 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 10)
    public void syncMarkRecordAmazon() {
        platformOrderAmazonSyncService.syncMarkRecord();
    }

    /**
     * 定时同步亚马逊库存回传状态
     */
    @Scheduled(cron = "50 11,31,51 * * * ?")
    public void syncMarkRecordAmazonStock_ToPlatform() {
        platformOrderAmazonSyncService.syncStockToPlatformRecord();
    }


    /**
     * 自动同步亚马逊订单
     */
//    @Scheduled(cron = "18 02,12,22,32,42,52 * * * ?")
    @Scheduled(cron = "18 12,32,52 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void amazonOrderSync() {
        platformOrderAmazonSyncService.platOrderAmazonSync();
    }


    /**
     * 自动确认乐天100订单
     */
    @Scheduled(cron = "0 18,48 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 20)
    public void platOrderAutoConfirm() {
        platformOrderRakutenSyncService.platOrderAutoConfirm();
    }


    /**
     * 自动同步乐天订单
     */
    @Scheduled(cron = "0 08,28,48 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void rakutenOrderSync() {
        platformOrderRakutenSyncService.platOrderRakutenSync();
    }


    /**
     * 自动同步乐天订单 历史
     */
    @Scheduled(cron = "0 58 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void rakutenOrderSyncHistory() {
//        platformOrderRakutenSyncService.platOrderRakutenSyncHistory();
    }


    /**
     * 自动同步shopify订单
     */
    @Scheduled(cron = "05 04,24,44 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void shopifyOrderSync() {
        platformOrderShopifySyncOrderService.platOrderShopifySync();
    }


    /**
     * 自动同步makeshop订单
     */
    @Scheduled(cron = "0 14,44 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void makeShopOrderSync() {
        platformOrderMakeshopSyncService.platOrderMakeShopSync();
    }

    /**
     * 自动同步qoo10订单
     */
    @Scheduled(cron = "0 11,31,51 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void qoo10OrderSync() {
        platformOrderQoo10Service.platOrderQoo10Sync();
    }


    /**
     * 自动同步qoo10取消订单
     */
    @Scheduled(cron = "0 01,31 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void qoo10ClaimOrderSync() {
        platformOrderQoo10Service.platOrderClaimQoo10Sync();
    }


    /**
     * 自动同步qoo10订单历史
     */
    @Scheduled(cron = "0 11 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void qoo10OrderSyncHistory() {
//        platformOrderQoo10Service.platOrderQoo10SyncHistory();
    }


    @Scheduled(cron = "0 0 0 * * ? ")
    public void removeProductStockRedisFlag() {
        redisTemplate.delete(RedisKeyConstants.ProductStock_INSERT_SKU);
    }

    /**
     * 仓储费每日计算
     */
    @Scheduled(cron = "10 1 0 * * ?")
    public void stockFee() {
        productStockService.getStockAgeList();
    }

    /**
     * 重置单号的序号
     */
    @Scheduled(cron = "0 0 0 1/7 * * ")
    public void resetOrderNoSeqNo() {
        redisTemplate.opsForValue().set(RedisKeyConstants.ORDER_INPUT_NO_SEQ, 1);
        redisTemplate.opsForValue().set(RedisKeyConstants.SHELF_NO_SEQ, 1);
        redisTemplate.opsForValue().set(RedisKeyConstants.ORDER_OUTBOUND_NO_SEQ, 1);
        redisTemplate.opsForValue().set(RedisKeyConstants.OUTBOUND_WAVE_NO_SEQ, 1);
        redisTemplate.opsForValue().set(RedisKeyConstants.CANCEL_OUTBOUND_NO_SEQ, 1);
        redisTemplate.opsForValue().set(RedisKeyConstants.CLAIM_NO_SEQ, 1);
    }

    /**
     * 每天四点执行
     */
    @Scheduled(cron = "0 0 4 * * ? ")
    public void executeEveryDay() {
        //认领单过期
        claimService.lambdaUpdate()
                .eq(ClaimDo::getStatus, ClaimStatusEnum.zero.getValue())
                .lt(ClaimDo::getExpireTime, DateUtil.date())
                .set(ClaimDo::getStatus, ClaimStatusEnum.three.getValue())
                .update();
        redisTemplate.opsForValue().set(RedisKeyConstants.WORK_ORDER_NO_SEQ, 1);
        redisTemplate.opsForValue().set(RedisKeyConstants.PLAN_BATCH_NO_SEQ, 1);
        Set<Long> rackIdList = stockRackService.lambdaQuery()
                .gt(ProductStockRackDo::getQuantity, 0)
                .or()
                .gt(ProductStockRackDo::getLockQuantity, 0)
                .list().stream().map(ProductStockRackDo::getRackId).collect(Collectors.toSet());
        if (CollUtil.isNotEmpty(rackIdList)) {
            storageLocationService.lambdaUpdate()
                    .in(StorageLocationDo::getId, rackIdList)
                    .set(StorageLocationDo::getEmptyLocationFlag, Boolean.FALSE)
                    .update();
            storageLocationService.lambdaUpdate()
                    .notIn(StorageLocationDo::getId, rackIdList)
                    .set(StorageLocationDo::getEmptyLocationFlag, Boolean.TRUE)
                    .update();
        } else {
            storageLocationService.lambdaUpdate()
                    .set(StorageLocationDo::getEmptyLocationFlag, Boolean.TRUE)
                    .update();
        }
    }
}